<%-- 
    Document   : index
    Created on : Jul 4, 2011, 6:07:19 PM
    Author     : northpole
--%>

<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <link type="text/css" rel="stylesheet" href="sidebar.css" media="screen" />
        <%boolean user=false;
        String str ="Visitor";
        session.setAttribute("username", str);
        
        
            if (request.getParameter("athenticate") != null) //if it isn't the 1st time you are called
                if (request.getParameter("athenticate").equals("true"))    //and if someone has tried to athenticate
                    
                
                if(!request.getParameter("username").equals("")&&   //and if everythings completed
                !request.getParameter("password").equals("")&&
                !request.getParameter("athenticate").equals("")){ %>
                
        <jsp:useBean id="userInfo" scope="session" class="user.User" />
        <jsp:setProperty name="userInfo" property="username" />
        <jsp:setProperty name="userInfo" property="password" />
         

        <%
                   String state=userInfo.athenticate();
                   if(state.equals("404"))
                    {
                    response.sendRedirect("ErrorPage.html");
                    return;
                   }
                   userInfo.loadOwnInfo();
                    userInfo.loadFriendList();
                    str = userInfo.getUsername();
                    ArrayList friends=userInfo.getFriendUsernames();
                    
                    int id = userInfo.getId();
                    if (!str.equals("Visitor")) {
                        session.setAttribute("username", str);
                        session.setAttribute("password",request.getParameter("password") );
                        session.setAttribute("id", id);
                        session.setAttribute("userInfo", userInfo);
                        session.setAttribute("name", userInfo.getName());
                        session.setAttribute("surname", userInfo.getSurname());
                        session.setAttribute("dateOfBirth", userInfo.getDateOfBirth());
                        session.setAttribute("interests", userInfo.getInterests());
                        session.setAttribute("welcomeText", userInfo.getWelcomeText());
                        session.setAttribute("friendList", friends);
                        user=true;
                        
                    }
                }

        %>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome To foo </title>
    </head>
    <body>
        <div  id="text">
        <%if(!user){    //if this is the first time you are called therefore user=false
            
        %>
        <h1>Please Login</h1>
        <form name="UsernamePassInputForm" action="index.jsp?athenticate=\"true\"">
            Username
            <input type="text" name="username" placeholder="Your Username"/>
            Password
            <input type="password" name="password"/>
            <input type="submit" value="Submit" name="Submit" />
            <input type="hidden" name="athenticate" value="true">
        </form>
        Want to Be part of the community?
        <a href="register.jsp">sign up</a>
      
      <% } else if(user)   {
          //else it means someone has already athenticated and redirect him to his profile
          if(str!=null){
          String redirectURL = "profile.jsp?username="+str;
          response.sendRedirect(redirectURL);
          return;
          }
          else
          {
             response.sendRedirect("ErrorPage.html");
              return;
          }
      }


        %>
        </div>
        OR You Can Search For Someone
          <div id ="searchbar">
                <form action="search.jsp" method="GET">
                    <input type="text" name="username" placeholder="search for usernames"/>
                    <a href="search.jsp"> Advanced Search</a>
                </form></div>
</body>
</html>
